package com.example.home.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.bean.ChangePassword;
import com.example.home.bean.Common;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

public class ChangePasswordActivity extends AppCompatActivity {

    private ChangePassword changePassword;
    private Handler handler;
    private EditText oldPass;
    private EditText newPass;
    private Button butChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initView();
    }

    private void initView() {
        handler = new Handler();
        oldPass = findViewById(R.id.oldPass);
        newPass = findViewById(R.id.newPass);
        butChange = findViewById(R.id.but_change);
        ImageView back = findViewById(R.id.back);

        butChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getchangePassPort();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void getchangePassPort() {
        String old_password = oldPass.getText().toString();
        String new_password = newPass.getText().toString();

        ChangePassword changePassword = new ChangePassword();
        changePassword.setOldPassword(old_password);
        changePassword.setNewPassword(new_password);


        OKHttpTool.doNetRequest("prod-api/api/common/user/resetPwd",
                OKHttpTool.PUT,
                Common.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        // 在成功时执行相应的操作

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                // 处理成功响应
                                // 密码修改成功的处理
                                Common common = (Common)obj;
                                if (common.getCode() == 200){
                                    //UI,更新UI的操作。主线程里面
                                    Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT).show();
                                    //跳转到登录
                                    Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    ChangePasswordActivity.this.finish();

                                    //updateToken();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 在失败时执行相应的操作
                    }
                },
                new Gson().toJson(changePassword)
        );
    }

    private void updateToken(){
        //账号密码
        ChangePasswordActivity.this.finish();
    }
}
